<?php

class News implements iPersistantData, iLookable, iHTMLViewable {

  private $_id;
  private $_author;
  private $_date;
  private $_title;
  private $_content;
  private $_picture;
  private $_season;


  public function __construct($a,$d,$t,$c,$p,$s,$id=-1)
  {
    $this->_id      = $id;
    $this->_author  = $a;
    $this->_date    = $d;
    $this->_title   = $t;
    $this->_content = $c;
    $this->_picture = $p;
    $this->_season  = $s;
  }

  public function getAuthor()  { return $this->_author;  }
  public function getTitle()   { return $this->_title;   }
  public function getContent() { return $this->_content; }

  public function setAuthor($v)  { $this->_author  = $v; }
  public function setTitle($v)   { $this->_title   = $v; }
  public function setContent($v) { $this->_content = $v; }

  /*********************
   ** iPersistantData **
   *********************/

  public static function createTable()
  {
    $myDb = Database::getSingleton();
    $query  = "CREATE TABLE IF NOT EXISTS `news`(\n";
    $query .= "  `id`      INTEGER      NOT NULL PRIMARY KEY auto_increment,\n";
    $query .= "  `author`  VARCHAR(50)  NOT NULL, \n";
    $query .= "  `date`    VARCHAR(10)  NOT NULL, \n";
    $query .= "  `title`   VARCHAR(50)  NOT NULL, \n";
    $query .= "  `content` LONGTEXT     NOT NULL, \n";
    $query .= "  `season`  INTEGER      NOT NULL, \n" ;
    $query .= "  `picture` VARCHAR(255) NOT NULL); ";
    $myDb->execute($query);
  }

  public static function dropTable()
  {
    $myDb = Database::getSingleton();
    $query = "DROP TABLE IF EXISTS `news`; ";
    $myDb->execute($query);
  }
  
  public function create()
  {
    $myDb = Database::getSingleton();
    if ($this->_id != -1)
      throw new RuntimeException("News/create()",
				 "object id is still defined !");
    $author  = Database::escape($this->_author);
    $date    = Database::escape($this->_date);
    $title   = Database::escape($this->_title);
    $content = Database::escape($this->_content);
    $picture = Database::escape($this->_picture);
    $season  = Database::escape($this->_season);

    $query = "INSERT INTO `news` VALUES (\n";
    $query .= "'','$author','$date','$title','$content','$season','$picture'); ";
    
    $result = $myDb->executeInsert($query);
    if ($result[0] != 0)
      $this->_id = $result[0];
    else
      $this->_id = -1;
  }

  public static function read($key)
  {
    $myDb  = Database::getSingleton();
    $key   = Database::escape($key);
    $query = "SELECT * FROM `news` WHERE `id` = $key; ";
    
    $ans = Database::assoc($myDb->execute($query));
    if (! $ans)
      throw new Exception("Unknown '$key'");
    $aNews = new News($ans["author"],$ans["date"],$ans["title"],
		      $ans["content"],$ans["picture"],$ans["season"],$ans["id"]);
    return $aNews;
  }

  public function update()
  {
    $myDb = Database::getSingleton();
    
    $id      = Database::escape($this->_id);
    $author  = Database::escape($this->_author);
    $date    = Database::escape($this->_date);
    $title   = Database::escape($this->_title);
    $content = Database::escape($this->_content);
    $picture = Database::escape($this->_picture);
    $season  = Database::escape($this->_season);

    $query = <<<EOS
UPDATE `news` SET
      `author`  = '$author',
      `date`    = '$date',
      `title`   = '$title',
      `content` = '$content',
      `season`  = '$season',
      `picture` = '$picture'
      WHERE id = '$id' ;
EOS;
      $myDb->execute($query);
  }

  public function delete()
  {
    $myDb = Database::getSingleton();
    $id = Database::escape($this->_id);
    $myDb->execute("DELETE FROM `news` WHERE `id` = $id; ");
  }

  public function tag()
  {
    $tmp = array();
    $tmp[0] = "News";
    $tmp[1] = $this->_id;
    return $tmp;
  }

  public function soundsLike($other)
  {
    return $this->_date == $other->_date && $this->_author == $other->_author;
  }

  /***************
   ** iLookable **
   ***************/

  public static function lookFor($filter)
  {
    $myDb = Database::getSingleton();
    $query = "SELECT * FROM `news` WHERE `season` = '$filter' ORDER BY `date` DESC;";
    $ans = $myDb->execute($query);
    if (! $ans)
      return False;
    return $ans;
  }

  public static function showResult($set,$url="")
  { 
    $content = "";
    while(($row = Database::assoc($set)) != False)  {
      $aNews = News::read($row["id"]);
      $content .= "<tr>" . $aNews->displayInline($url) . "</tr>\n";
    }
    if ($content == "") {
      $ans = "<div align=\"center\"> <span class=\"error\">";
      $ans .= "Aucune news !";
      $ans .= "</span></div>";
      return $ans;
    }
    $header = "<div align=\"center\"> <table class=\"resultset\">";
    $header .= "<tr> <th> Date </th> <th> Auteur </th> ";
    $header .= "<th> Titre </th> <th>  Contenu </th> </tr> \n";
    
    $footer = "</table> </div>";

    return $header . $content . $footer;
  }

  /*******************
   ** iHTMLViewable **
   *******************/

  public function displayLong() 
  {
    $id    = Database::escape($this->_id);
    $title = $this->_title;
    $author = $this->_author;
    $date = Date::fromSql($this->_date); $date = $date->prettyPrint();
    $content = nl2br($this->_content);
    $picture = $this->_picture;
    // TRAITER LES IMAGES ICI !

    $page = <<<EOS
<div align="center">
  <table class="resultset">
    <tr> <th colspan="2" > News #$id </th> </tr>
      <tr> <th> Titre  </th> <td> $title </td> </tr>
      <tr> <th> Auteur </th> <td> $author </td> </tr>
      <tr> <th> Date   </th> <td> $date </td> </tr>
  </table>
</div>
  <h2> Texte </h2>
$content
$picture
EOS;
    return $page;
  }

  public function displayShort()
  {
    return $this->displayLong();
  }

  public function displayInline($url=false)
  {
    $content = "<td>";
    $date = Date::fromSql($this->_date);
    if ($url == false)
      $content .= $date->displaySquizzed();
    else
      $content .= "<a href=\"?id=".$this->_id."\">" . 
	$date->displaySquizzed() . "</a>";
    $content .= "</td>";
    $content .= "<td>".$this->_author."</td>";
    $content .= "<td>" . $this->_title . "</td>";
    $content .= "<td>" . substr($this->_content,0,25) . " ...</td> \n"; 
    return $content;
  }

}
?>